package dashboard.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import dashboard.model.ParamMaster;
import dashboard.service.DebugDbCommandService;

@Controller
public class AdminController {

	private static final Log log = LogFactory.getLog(AdminController.class);
	
	private static final String CONFIGURE = "configurationpage";
	
	private static final String DEBUG = "debugpage";
	
	@Autowired
	protected DebugDbCommandService debugDbCommandService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView configure(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
		log.debug("start to configure the thresholds used to determine whether database is healthy");
		
		return new ModelAndView(CONFIGURE, "configuration", debugDbCommandService.getParamMaster());
	}
	
	@RequestMapping(method= RequestMethod.GET)
	public ModelAndView debug(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
		log.debug("execute sql!");
		
		return new ModelAndView(DEBUG);
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public @ResponseBody String executeSQL(@RequestParam("sqls")String sqls)
		throws ServletException, IOException{
		log.debug("execute sql!");
		
		return debugDbCommandService.debugUserInputCommandHtml(sqls);
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody String configure(@RequestParam("xUnit")double x, @RequestParam("yUnit")double y, 
			@RequestParam("spAttn")String spAttn,@RequestParam("spHealthy")String spHealthy,
			@RequestParam("bcAttn")String bcAttn,@RequestParam("bcHealthy")String bcHealthy,
			@RequestParam("rlAttn")String rlAttn,@RequestParam("rlHealthy")String rlHealthy,
			@RequestParam("smAttn")String smAttn,@RequestParam("smHealthy")String smHealthy){
		
		ParamMaster master = new ParamMaster();
		master.setxVal(x);
		master.setyVal(y);
		master.setSpAttn(Double.parseDouble(spAttn));
		master.setSpHealthy(Double.parseDouble(spHealthy));
		
		master.setBcAttn(Double.parseDouble(bcAttn));
		master.setBcHealthy(Double.parseDouble(bcHealthy));
		
		master.setRdAttn(Double.parseDouble(rlAttn));
		master.setRdHealthy(Double.parseDouble(rlHealthy));
		
		master.setSmAttn(Double.parseDouble(smAttn));
		master.setSmHealthy(Double.parseDouble(smHealthy));
		
		if(debugDbCommandService.updateParamMaster(master) == 1){
			return "Successful";
		}
		else{
			return "Fail";
		}
	}
}
